home *** CD-ROM | disk | FTP | other *** search
- unit AutoRes1;
- {
- *********************************************************
- * demo for use of def files with local tables at your *
- * client's site *
- * *
- * (c) 1996-97 Reinhard Kalinke *
- * *
- *********************************************************
- }
-
- {NOTE When compiling the samples or a project of your own using
- BDEDoRxS methods with Delphi 1 tests seem to indicate that you
- better increase stack size to 24 or even 32k.}
-
- (*
- This demo takes a number of start params so that it can be 'controlled'
- by the calling app. Order and meaning of params are:
-
- Alias (string, either holding an alias or a directory path)
- DoDeleteDefs (either 0 or 1, determining whether def files are deleted
- after processing)
- DoCreateTables (either 0 or 1, determining whether non-existing tables
- will be created)
- DoIndices (either 0 or 1, determining whether indices are processed)
- DoRefInt (either 0 or 1, determining whether refint checks are processed)
- DoValchecks (either 0 or 1, determining whether valchecks are processed)
- IndicesOnly (either 0 or 1, determining whether only indices will be processed)
-
- A typical call for a 'full-featured' restructure without deleting def files
- would look like this:
-
- WinExecAndWait('<path>AUTOREST.EXE MyAlias 0 1 1 1 1 0',SW_SHOW);
-
- You need to specify all start params, otherwise this sample would give an
- error message!
-
- Below is code for WinExecAndWait routines for 16 and 32bit:
-
- function WinExecAndWait(FileName: String; Visibility: Word): Word;
- var { by Pat Ritchey }
- zAppName:array[0..512] of char;
- InstanceID: THandle;
- Msg: TMsg;
- begin
- StrPCopy(zAppName,FileName);
- InstanceID := WinExec(zAppName, Visibility);
- if InstanceID < 32 then { a value less than 32 indicates an Exec error }
- Result := -1
- else
- begin
- repeat
- while PeekMessage(Msg, 0, 0, 0, pm_Remove) do
- begin
- if Msg.Message = wm_Quit then Halt(Msg.WParam);
- TranslateMessage(Msg);
- DispatchMessage(Msg);
- end;
- until GetModuleUsage(InstanceID) = 0;
- Result := 0;
- end;
- end;
-
- function WinExecAndWait32(FileName: String; Visibility: integer):integer;
- var { by Pat Ritchey }
- zAppName:array[0..512] of char;
- zCurDir:array[0..255] of char;
- WorkDir:String;
- StartupInfo:TStartupInfo;
- ProcessInfo:TProcessInformation;
- begin
- StrPCopy(zAppName,FileName);
- GetDir(0,WorkDir);
- StrPCopy(zCurDir,WorkDir);
- FillChar(StartupInfo,Sizeof(StartupInfo),#0);
- StartupInfo.cb := Sizeof(StartupInfo);
- StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
- StartupInfo.wShowWindow := Visibility;
- if not CreateProcess(nil,
- zAppName, { pointer to command line string }
- nil, { pointer to process security attributes }
- nil, { pointer to thread security attributes }
- false, { handle inheritance flag }
- CREATE_NEW_CONSOLE or { creation flags }
- NORMAL_PRIORITY_CLASS,
- nil, { pointer to new environment block }
- nil, { pointer to current directory name }
- StartupInfo, { pointer to STARTUPINFO }
- ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
- else
- begin
- WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
- GetExitCodeProcess(ProcessInfo.hProcess,Result);
- CloseHandle( ProcessInfo.hProcess );
- CloseHandle( ProcessInfo.hThread );
- Result := 0;
- end;
- end;
-
- *)
-
- interface
-
- uses
- WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, FileCtrl, Db, DBTables, Px7Table,
- IniFiles, ExtCtrls,
- {$IFDEF WIN32}
- ComCtrls,
- {$ELSE}
- Gauges,
- {$ENDIF}
- DBIProcs;
-
- type
- TMainForm = class(TForm)
- AbortBtn: TButton;
- RestTbl: TPx7Table;
- RestDB: TDatabase;
- Panel1: TPanel;
- Panel2: TPanel;
- Label1: TLabel;
- procedure FormShow(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure AbortBtnClick(Sender: TObject);
- procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure AbortBtnMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- private
- { Private-Deklarationen }
- FCalced: boolean;
- FBDEVersion: string;
- FPreventSizing: boolean;
- FDeleteVals: boolean;
- FAtWork: boolean;
- FAbort: boolean;
- FDirectory: TFileName;
- {$IFDEF WIN32}
- ProgressBar1: TProgressBar;
- {$ELSE}
- ProgressBar1: TGauge;
- {$ENDIF}
- procedure DoIt;
- procedure WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo);
- message WM_GETMINMAXINFO;
- procedure WMNCHitTest(var Msg: TWMNCHitTest);
- message WM_NCHitTest;
- procedure WMInitMenuPopup(var Msg: TWMInitMenuPopup);
- message WM_INITMENUPOPUP;
- public
- { Public-Deklarationen }
- end;
-
- var
- MainForm: TMainForm;
-
- implementation
-
- {$R *.DFM}
-
- uses BDEDoRxS;
-
- procedure AssignDBDir(ADataBase: TDataBase; const AFileName: TFileName);
- begin
- with ADataBase do
- if (Params.Count = 0)
- or (Params[0] <> 'PATH='+AFileName) then
- begin
- if Connected then Connected := False;
- DriverName := 'STANDARD'; {clears any alias as well}
- Params.Clear;
- Params.Add('PATH='+AFileName);
- Open;
- end;
- end;
-
- {'Wrappers' you might want to paste into your apps/restructors.
- For an example on how to use them check form method DoItBtnClick}
-
- {scans a dir for files with extension AExt and writes them
- into a list for further processing}
- function DoScanDirForFiles(const ADir,AExt: TFileName;
- AList: TStrings): integer;
- var FileRec: TSearchRec;
- ScanDir: TFileName;
- Res: integer;
- begin
- AList.Clear;
- if (ADir[Length(ADir)] <> '\') then ScanDir := ADir+'\'
- else ScanDir := ADir;
- Res := SysUtils.FindFirst(ADir+'*.'+AExt, 0, FileRec);
- while Res = 0 do
- begin
- AList.Add(ScanDir+FileRec.Name);
- Res := SysUtils.FindNext(FileRec);
- end;
- SysUtils.FindClose(FileRec);
- Result := AList.Count;
- end;
-
- {processes table defs with thw whole range of current
- options (indices, RI, Val)}
- procedure DoRestructureFromFile(AFileList: TStrings;
- ADataBase: TDataBase;
- ATable: TTable;
- {$IFDEF WIN32}
- AProgressBar: TProgressBar;
- {$ELSE}
- AProgressBar: TGauge;
- {$ENDIF}
- AStatusPanel: TPanel;
- const DoCreateTables,
- DoCreateIndices,
- DoCreateRefInt,
- DoCreateValchecks,
- DoDeleteDefs: boolean);
- var i,iProg,iPass,iPasses: integer;
- DefFile, DBFile: TFileName;
- DoIndex: boolean;
- ActionStr: string;
- begin
- Screen.Cursor := crHourGlass;
- try
- DoIndex := DoCreateIndices;
- if (DoCreateRefInt or DoCreateValchecks) then
- iPasses := 2 else iPasses := 1;
- {$IFDEF WIN32}
- AProgressBar.Max := AFileList.Count*(1+ord(DoIndex));
- {$ELSE}
- AProgressBar.MaxValue := AFileList.Count*(1+ord(DoIndex));
- {$ENDIF}
- for iPass:=1 to iPasses do
- begin
- iProg := 0;
- {$IFDEF WIN32}
- AProgressBar.Position := 0;
- {$ELSE}
- AProgressBar.Progress := 0;{}
- {$ENDIF}
- if (iPass = 1) then
- ActionStr := 'processing: '
- else
- ActionStr := 'creating RI and/or ValChecks: ';
- for i:=0 to pred(AFileList.Count) do
- begin
- DefFile := AFileList.Strings[i];
- with TIniFile.Create(DefFile) do
- try
- ATable.TableName := ReadString('Table','Name','');
- AStatusPanel.Caption := ActionStr+ATable.TableName;
- AStatusPanel.Update;
- if (iPass = 2) then
- begin
- ATable.Open;
- {'Bugfix' BDE4.0:}
- if MainForm.FDeleteVals then
- BDEDropValFile(ATable);
- if DoCreateRefInt then
- {dropping existing RI is included
- with below function}
- BDEAddRIFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;{}
- {$ENDIF}
- if DoCreateValchecks then
- {dropping existing val is included
- with below function}
- BDEAddValchecksFromFile(ATable, DefFile); {}
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;{}
- {$ENDIF}
- Continue;
- end
- else
- try
- ATable.Open;
- if DoCreateRefInt then
- BDEDropAllRIConstraints(ATable);
- if DoCreateIndices then
- BDEDropAllIndices(ATable);
- BDERestructTableFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;{}
- {$ENDIF}
- except
- on E:EDBEngineError do
- begin
- DoIndex := False;
- {if table does not exist:}
- if ((E.Errors[pred(E.ErrorCount)].ErrorCode = DBIERR_OSENOENT)
- or (E.Errors[pred(E.ErrorCount)].ErrorCode = DBIERR_NOSUCHTABLE))
- and DoCreateTables then
- begin
- BDECreateTableFromFile(ADataBase, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;{}
- {$ENDIF}
- DoIndex := DoCreateIndices;
- ATable.Open;
- DBISaveChanges(ATable.Handle);
- end
- else raise;
- end;
- else raise;
- end;
- if DoIndex then
- {dropping existing indices is included
- with below function}
- BDEAddIndicesFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;{}
- {$ENDIF}
- finally
- Free;
- ATable.Close;
- end;
- end;
- end;
- AStatusPanel.Caption := 'Done!';
- AStatusPanel.Update;
- finally
- Screen.Cursor := crDefault;
- end;
- if DoDeleteDefs then
- begin
- for i:=0 to pred(AFileList.Count) do
- SysUtils.DeleteFile(AFileList.Strings[i]);
- end;
- end;
-
- {processes table defs for field restructure and indices only
- (no RI or Val processing)}
- procedure DoSimpleRestructureFromFile(AFileList: TStringList;
- ADataBase: TDataBase;
- ATable: TTable;
- {$IFDEF WIN32}
- AProgressBar: TProgressBar;
- {$ELSE}
- AProgressBar: TGauge;
- {$ENDIF}
- AStatusPanel: TPanel;
- const DoCreateTables,
- DoCreateIndices,
- DoDeleteDefs: boolean);
- var i,iProg: integer;
- DefFile, DBFile: TFileName;
- DoIndex: boolean;
- Res: integer;
- FileRec: TSearchRec;
- begin
- Screen.Cursor := crHourGlass;
- try
- DoIndex := DoCreateIndices;
- iProg := 0;
- {$IFDEF WIN32}
- AProgressBar.Position := 0;
- AProgressBar.Max := AFileList.Count*(1+ord(DoIndex));
- {$ELSE}
- AProgressBar.Progress := 0;
- AProgressBar.MaxValue := AFileList.Count*(1+ord(DoIndex));
- {$ENDIF}
- for i:=0 to pred(AFileList.Count) do
- begin
- DefFile := AFileList.Strings[i];
- with TIniFile.Create(DefFile) do
- try
- ATable.TableName := ReadString('Table','Name','');
- AStatusPanel.Caption := 'processing: '+ATable.TableName;
- AStatusPanel.Update;
- try
- ATable.Open;
- if DoCreateIndices then
- BDEDropAllIndices(ATable);
- BDERestructTableFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;
- {$ENDIF}
- except
- on E:EDBEngineError do
- begin
- DoIndex := False;
- {if table does not exist:}
- if ((E.Errors[pred(E.ErrorCount)].ErrorCode = DBIERR_OSENOENT)
- or (E.Errors[pred(E.ErrorCount)].ErrorCode = DBIERR_NOSUCHTABLE))
- and DoCreateTables then
- begin
- BDECreateTableFromFile(ADataBase, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;
- {$ENDIF}
- DoIndex := DoCreateIndices;
- ATable.Open;
- DBISaveChanges(ATable.Handle);
- end
- else raise;
- end;
- else raise;
- end;
- if DoIndex then
- BDEAddIndicesFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;
- {$ENDIF}
- finally
- Free;
- ATable.Close;
- end;
- end;
- AStatusPanel.Caption := 'Done!';
- AStatusPanel.Update;
- finally
- Screen.Cursor := crDefault;
- end;
- if DoDeleteDefs then
- begin
- for i:=0 to pred(AFileList.Count) do
- SysUtils.DeleteFile(AFileList.Strings[i]);
- end;
- end;
-
- {processes defs for indices only}
- procedure DoProcessIndicesFromFile(AFileList: TStringList;
- ATable: TTable;
- {$IFDEF WIN32}
- AProgressBar: TProgressBar;
- {$ELSE}
- AProgressBar: TGauge;
- {$ENDIF}
- AStatusPanel: TPanel;
- const DoDeleteDefs: boolean);
- var i,iProg,iPass,iPasses: integer;
- DefFile, DBFile: TFileName;
- Res: integer;
- FileRec: TSearchRec;
- begin
- Screen.Cursor := crHourGlass;
- try
- iProg := 0;
- {$IFDEF WIN32}
- AProgressBar.Position := 0;
- AProgressBar.Max := AFileList.Count;
- {$ELSE}
- AProgressBar.Progress := 0;
- AProgressBar.MaxValue := AFileList.Count;
- {$ENDIF}
- for i:=0 to pred(AFileList.Count) do
- begin
- DefFile := AFileList.Strings[i];
- with TIniFile.Create(DefFile) do
- try
- ATable.TableName := ReadString('Table','Name','');
- AStatusPanel.Caption := 'creating indices: '+ATable.TableName;
- AStatusPanel.Update;
- ATable.Open;
- {dropping indices is included with below function}
- BDEAddIndicesFromFile(ATable, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;
- {$ENDIF}
- finally
- Free;
- ATable.Close;
- end;
- end;
- AStatusPanel.Caption := 'Done!';
- AStatusPanel.Update;
- finally
- Screen.Cursor := crDefault;
- end;
- if DoDeleteDefs then
- begin
- for i:=0 to pred(AFileList.Count) do
- SysUtils.DeleteFile(AFileList.Strings[i]);
- end;
- end;
-
- {processes index defs for a list of files in case of
- index errors ('Index out of date')}
- procedure DoRecoverIndicesFromFile(AFileList: TStringList;
- ADB: TDataBase;
- ATable: TTable;
- {$IFDEF WIN32}
- AProgressBar: TProgressBar;
- {$ELSE}
- AProgressBar: TGauge;
- {$ENDIF}
- AStatusPanel: TPanel;
- const DoDeleteDefs: boolean);
- var i,iProg,iPass,iPasses: integer;
- DefFile, DBFile: TFileName;
- Res: integer;
- FileRec: TSearchRec;
- begin
- Screen.Cursor := crHourGlass;
- try
- iProg := 0;
- {$IFDEF WIN32}
- AProgressBar.Position := 0;
- AProgressBar.Max := AFileList.Count;
- {$ELSE}
- AProgressBar.Progress := 0;
- AProgressBar.MaxValue := AFileList.Count;
- {$ENDIF}
- for i:=0 to pred(AFileList.Count) do
- begin
- DefFile := AFileList.Strings[i];
- with TIniFile.Create(DefFile) do
- try
- ATable.TableName := ReadString('Table','Name','');
- AStatusPanel.Caption := 'recovering indices: '+ATable.TableName;
- AStatusPanel.Update;
- BDERecoverIndicesFromFile(ADB, ATable.TableName, DefFile);
- inc(iProg);
- {$IFDEF WIN32}
- AProgressBar.Position := iProg;
- {$ELSE}
- AProgressBar.Progress := iProg;
- {$ENDIF}
- finally
- Free;
- end;
- end;
- AStatusPanel.Caption := 'Done!';
- AStatusPanel.Update;
- finally
- Screen.Cursor := crDefault;
- end;
- if DoDeleteDefs then
- begin
- for i:=0 to pred(AFileList.Count) do
- SysUtils.DeleteFile(AFileList.Strings[i]);
- end;
- end;
- {end of 'wrapper' section}
-
- procedure TMainForm.FormShow(Sender: TObject);
- begin
- if not FCalced then
- begin
- CalcControlSize(self);
- {$IFDEF WIN32}
- FBDEVersion := BDEGetIdapi32Version;
- {$ELSE}
- FBDEVersion := BDEGetIdapi16Version;
- {$ENDIF}
- FCalced := True;
- FPreventSizing := True;
- DoIt;
- end;
- end;
-
- procedure TMainForm.DoIt;
- var AFileList: TStringList;
- ADir: TFileName;
- begin
- if (ParamStr(7) <> '0') and (ParamStr(7) <> '1') then
- raise Exception.Create('Start-up params not complete');
- if (Pos('\',ParamStr(1)) = 0) then
- begin
- RestDB.Close;
- RestDB.Params.Clear;
- RestDB.AliasName := ParamStr(1);
- RestDB.Open;
- ADir := BDEGetDBPath(ParamStr(1));
- end
- else
- begin
- AssignDBDir(RestDB,ParamStr(1));
- ADir := ParamStr(1);
- end;
- FAtWork := True;
- AFileList := TStringList.Create;
- try
- if (ParamStr(7) = '1') then
- begin
- if (DoScanDirForFiles(ADir,'DBX',AFileList) > 0) then
- DoRecoverIndicesFromFile(AFileList,RestDB,RestTbl,
- ProgressBar1,Panel1,
- (ParamStr(2) = '1'))
- else
- ShowMessage('No files to process');
- end
- else
- begin
- if (DoScanDirForFiles(ADir,'DBI',AFileList) > 0) then
- DoRestructureFromFile(AFileList,RestDB,RestTbl,ProgressBar1,Panel1,
- (ParamStr(3) = '1'),(ParamStr(4) = '1'),
- (ParamStr(5) = '1'),(ParamStr(6) = '1'),
- (ParamStr(2) = '1'))
- else
- ShowMessage('No files to process');
- end;
- finally
- AFileList.Free;
- AbortBtn.Tag := 1;
- AbortBtn.Caption := 'Close';
- FAtWork := False;
- Screen.Cursor := crDefault;
- end;
- end;
-
- procedure TMainForm.AbortBtnClick(Sender: TObject);
- begin
- if (AbortBtn.Tag = 0) then
- FAbort := True
- else
- Application.Terminate;
- end;
-
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- {$IFDEF WIN32}
- ProgressBar1 := TProgressBar.Create(self);
- {$ELSE}
- ProgressBar1 := TGauge.Create(self);
- {$ENDIF}
- with ProgressBar1 do
- begin
- Parent := Panel2;
- Align := alClient;
- Visible := True;
- end;
- end;
-
- procedure TMainForm.WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo);
- begin
- inherited;
- if FPreventSizing then
- with (self), Msg.MinMaxInfo^ do
- begin
- ptMinTrackSize.x:= Width;
- ptMaxTrackSize.x:= Width;
- ptMinTrackSize.y:= Height;
- ptMaxTrackSize.y:= Height;
- end;
- end;
-
- procedure TMainForm.WMInitMenuPopup(var Msg: TWMInitMenuPopup);
- begin
- inherited;
- if FPreventSizing and Msg.SystemMenu then
- begin
- EnableMenuItem(Msg.MenuPopup, SC_SIZE, MF_BYCOMMAND or MF_GRAYED);
- EnableMenuItem(Msg.MenuPopup, SC_MAXIMIZE, MF_BYCOMMAND or MF_GRAYED);
- end;
- end;
-
- procedure TMainForm.WMNCHitTest(var Msg: TWMNCHitTest);
- begin
- inherited;
- if FPreventSizing then
- with Msg do
- if Result in [HTLEFT, HTRIGHT, HTBOTTOM, HTBOTTOMRIGHT,
- HTBOTTOMLEFT, HTTOP, HTTOPRIGHT, HTTOPLEFT] then
- Result := longint(HTNOWHERE);
- end;
-
- procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- begin
- if FAtWork then Screen.Cursor := crHourGlass;
- end;
-
- procedure TMainForm.AbortBtnMouseMove(Sender: TObject; Shift: TShiftState;
- X, Y: Integer);
- begin
- if FAtWork then Screen.Cursor := crDefault;
- end;
-
- end.
-